package com.chenfeng.ego.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.chenfeng.ego.commons.util.FtpUtil;
import com.chenfeng.ego.commons.util.IDUtils;
import com.chenfeng.ego.service.PicService;
@Service
public class PicServiceImpl implements PicService
{	//图片服务器地址
	@Value("${host}")
	private String host;
	//图片服务器
	@Value("${port}")
	private String port;
	@Value("${name}")
	private String username;
	@Value("${password}")
	private String password;
	@Value("${basePath}")
	private String basePath;
	@Value("${filePath}")
	private String filePath;
	//图片上传功能
	public Map<String, Object> upload(MultipartFile uploadFile) throws NumberFormatException, IOException
	{
		Map<String,Object> map = new HashMap<String,Object>();
		
		System.out.println(host+" "+port+"  "+username+"  "+password+"  "+basePath+"  "+filePath);
		String suffix = uploadFile.getOriginalFilename().substring(uploadFile.getOriginalFilename().indexOf("."));
		System.out.println(suffix);
		String fileName = IDUtils.genImageName()+suffix;
		boolean result = FtpUtil.uploadFile(host, Integer.parseInt(port), username, password, basePath, filePath, fileName, uploadFile.getInputStream());
		if(result)
		{
			map.put("error", 0);
			map.put("url", "http://"+host+"/"+fileName);
		}else
		{
			map.put("error", 1);
			map.put("message", "图片上传失败");
		}
		return map;
	}
	
}
